package com.maotai.thread;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.sql.Connection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.maotai.bean.CustomName;
import com.maotai.dao.QiangGouDao;

public class MaoTaiQueryGSDThread extends Thread {
	private Map<String, Object> mapList;
	private Connection c;

	public MaoTaiQueryGSDThread(Map<String, Object> mapList, Connection c) {
		super();
		this.mapList = mapList;
		this.c = c;
	}

	public Connection getC() {
		return c;
	}

	public void setC(Connection c) {
		this.c = c;
	}

	public Map<String, Object> getMapList() {
		return mapList;
	}

	public void setMapList(Map<String, Object> mapList) {
		this.mapList = mapList;
	}

	@Override
	public void run() {
		try {
			QiangGouDao qgs = new QiangGouDao();

			HttpGet httpGet = null;// 创建get请求
			HttpClient httpClient = null;
			HttpResponse response = null;
			HttpEntity entity = null;

			// 创建默认的httpClient实例.
			// httpClient = new DefaultHttpClient();
			httpClient = HttpClients.createDefault();
			// 执行请求
			String httpUrl = "http://www.ip138.com:8080/search.asp?mobile="
					+ mapList.get("code") + "&action=mobile";
			// if(address.contains("来安县"))httpUrl =
			// "https://restapi.amap.com/v3/assistant/inputtips?s=rsv3&key=462f27e00614a30baa9ed1864455213f&city=&callback=jsonp_932577_&platform=JS&logversion=2.0&sdkversion=1.4.0&appname=https://www.cmaotai.com/ysh5/page/PersonalCenter/Address/addAddress.html?datas=1&csid=4573AA39-D2F9-4942-9B68-12BD116F03E9&keywords="+address+"汊河"+CustomName.getRandomChar();
			httpGet = new HttpGet(httpUrl);
			httpGet.addHeader("Host","www.ip138.com:8080");
			httpGet.addHeader("Connection","keep-alive");
			httpGet.addHeader("Pragma","no-cache");
			httpGet.addHeader("Cache-Control","no-cache");
			httpGet.addHeader("Upgrade-Insecure-Requests","1");
			httpGet.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
			httpGet.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
			httpGet.addHeader("Referer","http://www.ip138.com:8080/search.asp?mobile="
					+ mapList.get("code") + "&action=mobile");
			httpGet.addHeader("Accept-Encoding","");
			httpGet.addHeader("Accept-Language","");
			httpGet.addHeader("Cookie","pgv_pvi=3903874048; pgv_si=s871416832; ASPSESSIONIDACTQDBTC=NKEPIGHBOBHADJFMPODKOOOH");
			response = httpClient.execute(httpGet);
			entity = response.getEntity();
			String content = EntityUtils.toString(entity);
			content = new String(content.getBytes("ISO-8859-1"),"gb2312");
//			System.out.println(content);
			String returnStr = "";
			Pattern p1 = Pattern.compile("<TD width=\"138\" align=\"center\">卡号归属地</TD><td align=\"center\" class=tdc2><!-- <td></td> -->(.*)</TD>");  
			Matcher m = p1.matcher(content); 
			while(m.find()){  
				returnStr += m.group(1);
			}
			System.out.println(returnStr); 
			returnStr = returnStr.replace("&nbsp;", "");
			mapList.put("gsd", returnStr);
			qgs.updateGSD(c,mapList);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

}
